﻿#include "OperNastranBDFExport.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKAppFramework/FITKComponents.h"
#include "FITK_Kernel/FITKCore/FITKAbstractDataObject.h"
#include "FITK_Kernel/FITKCore/FITKThreadPool.h"
#include "FITK_Component/FITKNastranBDFIO/FITKNastranBDFIOInterface.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsFEModel.h"
#include "OperatorsInterface/GraphEventOperator.h"
#include "GUIFrame/MainWindow.h"
#include <QFileDialog>

namespace ModelOper
{
    bool OperNastranBDFExport::execGUI()
    {
        //选择文件
        this->clearArgs();
        //主界面
        QWidget* mw = FITKAPP->getGlobalData()->getMainWindow();
        //获取工作目录
        QString wk;
        AppFrame::FITKAppSettings* settings = FITKAPP->getAppSettings();
        if (settings)
            wk = settings->getWorkingDir();

        QFileDialog dlg(mw, QObject::tr("Nastran BDF File"), wk, "BDF(*.bdf)");
        dlg.setAcceptMode(QFileDialog::AcceptSave);  //保存模式
        dlg.show();
        //阻塞，否则键盘事件处理会出问题
        bool accept = false;
        QEventLoop loop;
        connect(&dlg, &QFileDialog::accepted, [&] {loop.quit(); accept = true;  });
        connect(&dlg, &QFileDialog::rejected, [&] {loop.quit(); accept = false;  });
        loop.exec();

        if (!accept) return false;
        QStringList files = dlg.selectedFiles(); //选择的文件名称
        if (files.isEmpty()) return false;
        QString fileName = files.at(0);

        if (fileName.isEmpty())
            return false;
        //存储信息
        if (_emitter == nullptr) return false;

        this->setArgs("FileName", fileName);
        return true;
    }



    bool OperNastranBDFExport::execProfession()
    {
        //获取文件名
        QString fileName;
        if (!this->argValue<QString>("FileName", fileName) || fileName.isEmpty()) return false;
        //获取IO接口
        Nastran::FITKNastranBDFIOInterface* bdfIO = FITKAPP->getComponents()->
            getComponentTByName<Nastran::FITKNastranBDFIOInterface>("NastranBDFIO");

        if (bdfIO == nullptr) return false;

        GUI::MainWindow* mw = FITKAPP->getGlobalData()->getMainWindowT<GUI::MainWindow>();
        if (mw)
            mw->setProcessLabel(QString(tr("Writeing %1")).arg(fileName));

        //获取FE模型
        Acoustics::FITKAcousticsDiscModel* model =
            FITKAPP->getGlobalData()->getMeshData<Acoustics::FITKAcousticsDiscModel>();
        if (model == nullptr) return false;

        //设置参数
        bdfIO->setDataObject("Model", model->getFEModel());
        bdfIO->setFileName(fileName);
        //等待线程结束
        Core::FITKThreadPool::getInstance()->wait();
        //关联槽函数
        connect(bdfIO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()), Qt::UniqueConnection);
        //执行写入
        bdfIO->exec(2);

        //保存执行的脚本
        QString scr = QString("Global.Files.ExportBDFFile('%1')").arg(fileName);
        this->saveScript(scr);

        return bdfIO->isSuccess();
    }

    void OperNastranBDFExport::ioThreadFinishedSlot()
    {
        GUI::MainWindow* mw = FITKAPP->getGlobalData()->getMainWindowT<GUI::MainWindow>();
        if (mw) mw->setProgressValue(100);

        Nastran::FITKNastranBDFIOInterface* bdfIO = FITKAPP->getComponents()->
            getComponentTByName<Nastran::FITKNastranBDFIOInterface>("NastranBDFIO");
        if (bdfIO == nullptr) return;
        //取消关联槽函数
        disconnect(bdfIO, SIGNAL(ioThreadFinishedSig()), this, SLOT(ioThreadFinishedSlot()));
    }

}
